﻿using Logistics.Commn;
using Logistics.Contract;
using Logistics.EntityFrameworkCore;
using Logistics.examination;
using Microsoft.AspNetCore.Mvc;
using System;
using System.IO;

namespace Logistics.Web.Controllers
{
    public class CurrencyContractController : Controller
    {
        private readonly ICurrencyContractAppService _currency;
        private readonly IApprovalactivityAppService _app;
        private readonly LogisticsDbContext db;
        public CurrencyContractController(ICurrencyContractAppService currency, IApprovalactivityAppService app, LogisticsDbContext dbc)
        {
            _currency = currency;
            _app = app;
            db = dbc;
        }
        public IActionResult Index()
        {
            return View();
        }
        public IActionResult GetList(int page = 1, int limit = 3, string Title = "", string Time = "")
        {
            var currencySearchDto = new CurrencySearchDto
            {
                SkipCount = (page - 1) * limit,
                MaxResultCount = limit,
                Sorting = "Id desc",
                Title = Title,
                Time = Time,
            };
            var result = _currency.GetDapperCurrencys(currencySearchDto).Result;
            //var result = _carryAppService.GetCarrys(carrySearchDto).Result;
            var count = result.TotalCount;
            return Ok(new { data = result.Items, count, code = "0", msg = "" });
        }
        public IActionResult DeleteCurrencybyId(int id)
        {
            var result = _currency.Delete(id);
            return Ok(result);
        }
        public IActionResult AddCurrency()
        {
            return View();
        }
        public IActionResult FileUpload()
        {
            // 上传后的文件
            var filePost = HttpContext.Request.Form.Files[0];
            // 文件名
            string fileName = filePost.FileName;
            // 服务器上文件要保存的路径
            string TruePath = Directory.GetCurrentDirectory() + "/files/" + fileName;
            // 保存文件
            using (FileStream fs = System.IO.File.Create(TruePath))
            {
                filePost.CopyTo(fs);
            }
            RedisHelper.Set("files", "/files/" + fileName);
            return Ok(new { data = "/files/" + fileName, count = 0, code = "0", msg = "" });
        }
        public ActionResult CreateCurrency(CurrencyDto cd)
        {
            int num = Convert.ToInt32(RedisHelper.Get("CarryNumber"));
            cd.NumberId = "TY-" + (num + 1);
            RedisHelper.Set("CarryNumber", num + 1);
            cd.CreationTime = DateTime.Now;
            var result = _currency.Create(cd);
            if (result.Result > 0)
            {
                return Redirect("/CurrencyContract/Index");
                //return Content("<script>location.href='/CarryContract/Index'</script>");
            }
            else
            {
                return Content("<script>layer.msg('添加失败');location.reload()</script>");
            }
        }
        public IActionResult Edit(int Id = 1)
        {
            ViewBag.Id = Id;
            return View();
        }
        public IActionResult GetCurrencyById(int Id)
        {
            var list = _currency.GetCurrencyById(Id).Result;
            return Ok(list);
        }
        public IActionResult UpdateCurrency(CurrencyDto cd)
        {
            var result = _currency.Update(cd).Result;
            if (result > 0)
            {
                return Redirect("/CurrencyContract/Index");
            }
            else
            {
                return Redirect("/CurrencyContract/Edit?Id=" + cd.Id);
            }
        }
        public IActionResult UP(int id, int TId)
        {
            int result = _app.Create(id, TId).Result;
            var list = db.Currency.Find(id);
            list.State = 1;
            db.Entry(list).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            return Ok(result);
        }
        public IActionResult Show(int Id)
        {
            ViewBag.Id = Id;
            return View();
        }
    }
}
